package lacus.wd_cinema.controller.api;

import jakarta.servlet.http.HttpServletRequest;
import lacus.wd_cinema.dto.movies.MovieAddRequest;
import lacus.wd_cinema.dto.movies.MovieAddResponse;
import lacus.wd_cinema.dto.movies.MovieUpdateRequest;
import lacus.wd_cinema.dto.movies.MovieUpdateResponse;
import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.service.AuthService;
import lacus.wd_cinema.service.MovieService;
import org.springframework.web.bind.annotation.*;

/**
 * 电影接口控制类
 * @author lacus
 * @version 0.5
 * @since 0.4
 */
@RestController
@RequestMapping("/api/movies")
public class MovieController {
    private final MovieService movieService;
    private final AuthService authService;

    public MovieController(MovieService movieService, AuthService authService) {
        this.movieService = movieService;
        this.authService = authService;
    }

    /** 添加新的电影 */
    @PostMapping
    public MovieAddResponse addMovie(
            @RequestBody MovieAddRequest request,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        return movieService.addMovie(request, operator);
    }

    /** 对电影进行更改 */
    @PutMapping("/{id}")
    public MovieUpdateResponse updateMovie(
            @PathVariable long id,
            @RequestBody MovieUpdateRequest request,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        return movieService.updateMovie(id, request, operator);
    }

    /** 删除电影 */
    @DeleteMapping("/{id}")
    public void deleteMovie(
            @PathVariable long id,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        movieService.deleteMovie(id, operator);
    }
}
